<?php
require_once("db_functions.php");
require_once("template.parser.class.php");

if(!MAX_SEARCH_LIST)
define("MAX_SEARCH_LIST",50);

class adrese{
	var $db;
	function adrese(){
		$this->db = new db_functions();
	}
	/**
	 * Fetch all regions
	 *
	 * @return Array(Array(region_id,region_name))
	 */
	function getRegions(){
		$res = $this->db->select2("SELECT * FROM regioni ORDER BY region_name");
		return $res;
	}
	/**
	 * Fetch opstine
	 *
	 * @param int region_id
	 * @return Array(Array(opstina_id,opstina_name))
	 */
	function getOpstine($regid=0){
		$q = "SELECT * FROM opstine";
		if($regid){
			$q .= " WHERE opstina_region_id=$regid";
		}
		$q .= " ORDER BY opstina_name";
		return $this->db->select2($q);
	}
	function getLokacijeAll($type=1,$opsid=0){
		$q = "SELECT * FROM lokacije WHERE lok_tip=$type AND lok_naziv !=''";
		if($opsid)
			$q .= " AND lok_parent=$opsid";
		$q .= " ORDER BY lok_naziv";
		return $this->db->select2($q);
	}
	/**
	 * Fetch mesto, opstina and naselje for the location id
	 *
	 * @param int $location_id
	 * @return Array(Mesto, Opstina, Naselje)
	 */
	function getLokacije($lid){
		$retnames = array("","mesto","opstina","naselje");
		$ret=array();
		do{
			$lok = $this->db->select2("SELECT * FROM lokacije WHERE lid=$lid");
			if(count($lok)){
				$lok = $lok[0];
				
				$tip = $lok["lok_tip"];
				$name = $lok["lok_naziv"];
				$lid = $lok["lid"];
				$parent =$lok["lok_parent"];
				$ptt = $lok["lok_ptt"];
				
				$key = $retnames[$tip];
				$ret[$key]["id"] = $lid;
				$ret[$key]["name"]= $name;
				$ret[$key]["parent"]= $parent;
				$ret[$key]["ptt"] = $ptt;
				
				$lid = $parent;
				$tip--;
			}else{
				break;
			}
		}while($tip > 0);
		return $ret;
	}
	/**
	 * Fetch ulica, broj, lokacija(Mesto, Opstina, Naselje)
	 *
	 * @param int $adrid
	 * @return Array(ulica,broj,lokacija(Mesto, Opstina, Naselje))
	 */
	function getAll($adrid){
		if(!isset($adrid)){
			return false;
		}
		$ret = array();
		$q = "SELECT ulice.ulica_ime as ulica, ulice.ulica_id as ulid, adrese.adresa_broj as broj, adrese.adresa_lid as lid "
			."FROM ulice, adrese WHERE adrese.adresa_ul_id=ulice.ulica_id AND adrese.adresa_id=$adrid LIMIT 1";
		list($res) = @$this->db->select2($q);
		if($res){
			$ret["ulica"] = $res["ulica"];
			$ret["ulica_id"] = $res["ulid"];
			$ret["broj"] = $res["broj"];
			$ret["lokacija"] = @$this->getLokacije($res["lid"]);
			
			$q = "SELECT region_id, region_name, opstina_name "
				."FROM opstine, regioni "
				."WHERE opstina_region_id=region_id AND opstina_id=".$ret["lokacija"]["mesto"]["parent"]." LIMIT 1";
			list($res) = @$this->db->select2($q);
			$ret["opstina"] = array("id"=>$ret["lokacija"]["mesto"]["parent"],"name"=>$res["opstina_name"]);
			$ret["region"]  = array("id"=>$res["region_id"],"name"=>$res["region_name"]);
		}else{
			$ret = false;
		}
		
		return $ret;
	}
	/**
	 * Fetch mesta from desired region
	 *
	 * @param int $regid
	 * @return mesta(id,name,ptt)
	 */
	function getMesta($opsid){
		if(!isset($opsid)){
			return false;
		}
		$q = "SELECT * FROM lokacije WHERE lok_parent=$opsid AND lok_tip=1 ORDER BY lok_naziv";
		$res = $this->db->select2($q);
		if($res){
			foreach ($res as $lok){
				$ret[] = array(
					"id" => $lok["lid"],
					"name" => $lok["lok_naziv"],
					"ptt"=> $lok["lok_ptt"]
				);
			}
			return $ret;
		}else{
			return false;
		}
	}
	/**
	 * Fetch lok.opstine from desired mesto
	 *
	 * @param int $mesid
	 * @return lok.opstine(id,name,ptt)
	 */
	function getLokOpstine($mesid){
		if(!isset($mesid)){
			return false;
		}
		$res = $this->db->select2("SELECT * FROM lokacije WHERE lok_parent=$mesid AND lok_tip=2 ORDER BY lok_naziv");
		if($res){
			foreach ($res as $row){
				$ret[] = array(
					"id"	=> $row["lid"],
					"name"	=> $row["lok_naziv"],
					"ptt"	=> $row["lok_ptt"]
				);
			}
			return $ret;
		}else{
			return false;
		}
	}
	/**
	 * Fetch lok.opstine from mesto
	 *
	 * @param int lok_opstina_id
	 * @return naselje(id,name,ptt)
	 */
	function getNaselja($lokopsid){
		if(!isset($lokopsid)){
			return false;
		}
		$res = $this->db->select2("SELECT * FROM lokacije WHERE lok_parent=$lokopsid AND lok_tip=3 ORDER BY lok_naziv");
		if($res){
			foreach ($res as $row){
				$ret[] = array(
					"id"	=> $row["lid"],
					"name"	=> $row["lok_naziv"],
					"ptt"	=> $row["lok_ptt"]
				);
			}
			return $ret;
		}else{
			return false;
		}
	}
	/**
	 * Fetch address ID
	 *
	 * @param Array(ulica_id,ulica)
	 * @param string broj
	 * @param int region
	 * @param int opstina
	 * @param int grad
	 * @param int podopstina
	 * @param int naselje
	 * 
	 * @return Array(ret_code,adr_id)
	 * ret codes:
	 * 		 0 -> update address
	 * 		-1 -> ulica changed
	 * 		-2 -> lokacija changed
	 */
	function getAddressId($ulica_id,$broj,$region,$opstina,$grad,$podopstina,$naselje){
		if($naselje != -1){
			$lid = $naselje;
			$lok_parent = $podopstina;
			$lok_tip = 3;
		}else{
			if($podopstina != -1){
				$lid = $podopstina;
				$lok_parent = $grad;
				$lok_tip = 2;
			}else{
				$lid = $grad;
				$lok_parent = $opstina;
				$lok_tip = 1;
			}
		}
		
		// get adr_id
		$q = "SELECT adresa_id FROM adrese WHERE adresa_ul_id=$ulica_id AND adresa_lid=$lid AND adresa_broj COLLATE cp1250_bin LIKE '$broj'";
		$adr_id = $this->db->getOne($q);
		return $adr_id;
	}
	function getUlicaId($ulica){
		$ulid = $this->db->getOne("SELECT ulica_id FROM ulice WHERE ulica_ime='$ulica'");
		return $ulid;
	}
	function addUlica($ulica){
		return $this->db->alterTable("INSERT INTO ulice(ulica_ime) VALUES('$ulica')");
	}
	function addAdresa($ulid,$lid,$broj){
		return $this->db->alterTable("INSERT INTO adrese(adresa_ul_id, adresa_lid,adresa_broj) VALUES('$ulid','$lid','$broj')");
	}
	function searchUlica($ul_name,$dest,$dest_hidden,$search_ul_type,$lok){
		
		$parser = new template_parser();
		$parser->assign("search_kword",$ul_name." ");
		
		$q = "SELECT * FROM ulice WHERE ulica_ime LIKE '%".addslashes($ul_name)."%' ORDER BY ulica_ime";
		$res = $this->db->select2($q);
		if(count($res[0])){
			foreach ($res as $ulica){
				$ulid = $ulica["ulica_id"];
				
				$q = "SELECT lid, lok_tip, lok_naziv, lok_ptt, ullok_desc FROM ulice_lokacije, lokacije WHERE ulice_lokacije.ullok_ul_id=$ulid AND ulice_lokacije.ullok_lok_id=lokacije.lid ORDER BY lok_naziv";
				$ullok = $this->db->select2($q);
				if ($ullok !== false) {
					foreach ($ullok as $row){
						$ul_ids[] = $ulid;
						$ul_names[] = $ulica["ulica_ime"];
						$ul_names_slash[] = addslashes($ulica["ulica_ime"]);
						$ul_lid[] = $row["lid"];
						$ul_lokname[] = " - ".$row["lok_naziv"];
						$ul_loktip[] = $row["lok_tip"];
					}
				}else{
					$ul_ids[] = $ulid;
					$ul_names[] = $ulica["ulica_ime"];
					$ul_names_slash[] = addslashes($ulica["ulica_ime"]);
					$ul_lid[] = "";
					$ul_lokname[] = "";
					$ul_loktip[] = "";
				}
			}
			$parser->assign("list_num",$ul_ids);
			$parser->assign("list_item",$ul_names);
			$parser->assign("list_item_slash",$ul_names_slash);
			$parser->assign("ln_grad",$lok["grad"]);
			$parser->assign("ln_lops",$lok["opstina"]);
			$parser->assign("ln_nas",$lok["naselje"]);
			$parser->assign("ln_lid",$ul_lid);
			$parser->assign("ln_name",$ul_lokname);
			$parser->assign("ln_type",$ul_loktip);
			$parser->assign("search_length",count($res));
		}else{
			$parser->assign("comment_start","<!--");
			$parser->assign("comment_end","-->");
			$parser->assign("search_length","0 ");
		}
		$parser->assign("dest",$dest);
		$parser->assign("dest_hidden",$dest_hidden);
		$parser->assign("search_ul_type",$search_ul_type);
		echo $parser->parseTemplate("search_ulica.tpl");
	}
}
?>